<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>RVM : Method Compilation</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
    </head>

    <body>
	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
		    <tr>
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            RVM : Method Compilation
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Jul 02, 2008 by <font color="#0050B2">dgrove</font>.
				    </div>

				    <p>The fundamental unit for optimization in Jikes RVM is a single method.  The optimization of a method consists of a series of  compiler phases performed on the method. These phases transform the <a href="IR.html" title="IR">IR</a> (intermediate representation) from bytecodes through HIR (high-level intermediate representation), LIR (low-level intermediate representation), and MIR (machine intermediate representation) and finally into machine code.  Various optimizing transformations are performed at each level of IR.</p>

<p>An object of the class <tt>CompilationPlan</tt> contains all the information necessary to generate machine code for a method. An instance of this class includes, among other fields, the <tt>RVMMethod</tt> to be compiled and the array of <tt>OptimizationPlanElements</tt> which define the compilation steps. The <tt>execute</tt> method of an <tt>CompilationPlan</tt> invokes the optimizing compiler to generate machine code for the method, executing the compiler phases as listed in the plan's <tt>OptimizationPlanElements</tt>.</p>

<p>The <tt>OptimizationPlanner</tt> class defines the standard phases used in a compilation. This class contains a static field, called <tt>masterPlan</tt>, which contains all possible <tt>OptimizationPlanElements</tt>. The structure of the master plan is a tree. Any element may either be an atomic element (a leaf of the tree), or an aggregate element (an internal node of the tree). The master plan has the following general structure:</p>
<ul>
	<li>elements which convert bytecodes to HIR</li>
	<li>elements which perform optimization transformations on the HIR
	<ul>
		<li>elements which perform optimization transformations using SSA form</li>
	</ul>
	</li>
	<li>elements which convert HIR to LIR</li>
	<li>elements which perform optimization transformations on the LIR
	<ul>
		<li>elements which perform optimization transformations using SSA form</li>
	</ul>
	</li>
	<li>elements which convert LIR to MIR</li>
	<li>elements which perform optimization transformations on MIR</li>
	<li>elements which convert MIR to machine code</li>
</ul>


<p>A client (compiler driver) constructs a specific optimization plan by including all the <tt>OptimizationPlanElements</tt> contained in the master plan which are appropriate for this compilation instance. Whether or not an element should be part of a compilation plan is determined  by its <tt>shouldPerform</tt> method. For each atomic element, the values in the <tt>OptOptions</tt> object are generally used to determine whether the element should be included in the compilation plan. Each aggregate element must be included when any of its component elements must be included.</p>

<p>Each element must have a <tt>perform</tt> method defined which takes the IR as a parameter. It is expected, but not required, that the <tt>perform</tt> method will modify the IR.  The perform method of an aggregate element will invoke the perform methods of its elements.</p>

<p>Each atomic element is an object of the final class <tt>OptimizationPlanAtomicElement</tt>. The main work of this class is performed by its <em>phase</em>, an object of type <tt>CompilerPhase</tt>. The <tt>CompilerPhase</tt> class is not final; each phase overrides this class, in particular it overrides the <tt>perform</tt> method, which is invoked by its  enclosing element's <tt>perform</tt> method. All the state associated with  the element is contained in the <tt>CompilerPhase</tt>; no state is in the element.</p>

<p>Every optimization plan consists of a selection of elements from the master plan; thus two optimization plans associated with different methods  will share the same component element objects.  Clearly, it is undesirable to share state  associated with a particular compilation phase between two different method compilations. In order to prevent this, the <tt>perform</tt> method of an atomic element creates a new instance of its phase immediately  before calling the phase's <tt>perform</tt> method.  In the case where the phase contains no state the <tt>newExecution</tt> method of <tt>CompilerPhase</tt> can be overridden to return the phase itself rather than a clone of the phase</p>

				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="http://docs.codehaus.org/images/border/border_bottom.gif"><img src="images/border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Jun 10, 2009 09:12</font></td>
		    </tr>
	    </table>
    </body>
</html>